package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShopingCartMapper;
import com.sky.vo.ShoppingCartADDVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ShopingCartServiceImpl implements ShopingCartService{
    @Autowired
    private UserShopingCartMapper userShopingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    private int initnumber = 1;
    public List<ShoppingCart> getlist(){
        return userShopingCartMapper.getlist();
    }
    @Transactional
    public void add(ShoppingCartDTO dto){
        //判断一下新增的菜品或套餐是否已在购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userid = BaseContext.getCurrentId();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(userid);
        List<ShoppingCart> list = userShopingCartMapper.getBydishId_or_setmealId_or_userid(shoppingCart);
        //若存在，则更新数量
        if(list!=null && list.size() > 0){
            ShoppingCart shoppingCart1 = new ShoppingCart();
            shoppingCart1.setUserId(userid);
            BeanUtils.copyProperties(dto,shoppingCart1);
            shoppingCart1.setNumber(list.get(0).getNumber() + 1);//给对应的已存在的数量加1
            userShopingCartMapper.update(shoppingCart1);
        } else{
            //若不存在，插入数据库
            BigDecimal amount = new BigDecimal(0);
            ShoppingCartADDVO vo = new ShoppingCartADDVO();
            if(dto.getSetmealId() == null){
                vo = dishMapper.get(dto.getDishId());
            }//新增菜品
            else{
                vo = setmealMapper.get(dto.getSetmealId());
            }//新增套餐
            ShoppingCart data = new ShoppingCart();
            BeanUtils.copyProperties(dto,data);
            BeanUtils.copyProperties(vo,data);
            data.setDishFlavor(dto.getDishFlavor());
            data.setAmount(vo.getPrice());
            data.setUserId(BaseContext.getCurrentId());
            data.setCreateTime(LocalDateTime.now());
            data.setNumber(initnumber);
            userShopingCartMapper.add(data);
        }

    }

    public void clean(){
        BigInteger userid = new BigInteger(BaseContext.getCurrentId().toString());
        userShopingCartMapper.clean(userid);
    }

    public void sub(ShoppingCartDTO dto){
        //判断一下要删除的菜品或套餐在购物车中是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userid = BaseContext.getCurrentId();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(userid);
        List<ShoppingCart> list = userShopingCartMapper.getBydishId_or_setmealId_or_userid(shoppingCart);
        //若存在，则数量减一
        if(list!=null && list.size() >= 2){
            ShoppingCart shoppingCart1 = new ShoppingCart();
            shoppingCart1.setUserId(userid);
            BeanUtils.copyProperties(dto,shoppingCart1);
            shoppingCart1.setNumber(list.get(0).getNumber() - 1);//给对应的已存在的数量加1
            userShopingCartMapper.update(shoppingCart1);
        }else{
            //若不存在直接删除
            BigInteger userId = new BigInteger(BaseContext.getCurrentId().toString());
            if(dto.getDishId() == null){
                userShopingCartMapper.subBysetmealId(dto.getSetmealId(),userId);
            } //删的是套餐
            else{
                userShopingCartMapper.subBydishId(dto.getDishId(),userId);
            }//删的是菜品
        }


    }

}
